package
{
	import flash.filters.GlowFilter;
	import flash.net.SharedObject;
	
	import org.flixel.*;
	import org.flixel.system.FlxSound;
	
	public class MenuState extends FlxState
	{
		protected var toContinue:FlxSprite;
		protected var newGame:FlxSprite;
		protected var credits:FlxSprite;
		protected var playerPuzzles:FlxSprite;
		protected var introVideo:FlxSprite;
		protected var mapEditor:FlxSprite;
		protected var exitButton:FlxSprite;
		protected var musicIcon:Music;
		protected var localInformation:SharedObject;
		protected var theCurrentLevel:int;
		protected var playIt:Boolean;
		
		protected var toContinueOverlapped:Boolean;
		protected var newGameOverlapped:Boolean;
		protected var creditsOverlapped:Boolean;
		protected var playerPuzzlesOverlapped:Boolean;
		protected var introVideoOverlapped:Boolean;
		protected var editorOverlapped:Boolean;
		protected var exitToRoomOverlapped:Boolean;
		
		protected var videoThing:UberAwesomeVideo;
		protected var hoverFilter:GlowFilter;
		
		public function MenuState()
		{
//			videoThing = new UberAwesomeVideo(1);
//			toContinueOverlapped = false;
//			newGameOverlapped = false;
//			introVideoOverlapped = false;
//			editorOverlapped = false;
			localInformation = SharedObject.getLocal("elementalGameInfo");

//			var level:String = localInformation.data["currentLevel"];
//			if (level != null) {
//				theCurrentLevel = int(level);
//			} else {
//				localInformation.data["currentLevel"] = "1";
//				localInformation.flush();
//				theCurrentLevel = int(localInformation.data["currentLevel"]);
//			}
			
			super();
			this.add(new FlxSprite(0,0,Res.Titlescreen));

			toContinue = new FlxSprite(162, 260, Res.continueButton);
			newGame = new FlxSprite(162, 320, Res.newGameButton);
			
			introVideo = new FlxSprite(162, 380, Res.videoButton);
			credits = new FlxSprite(162, 560, Res.creditsButton);
			
			musicIcon = new Music(676,0);
			
			
			if (Elemental.MODE == Elemental.FACEBOOK_MODE) {
				exitToRoomOverlapped = false;
				exitButton = new FlxSprite(162, 520, Res.exitToRoomButton);
				//toContinue.y += 100;
				add(toContinue);
				//newGame.y += 100;
				credits.y -= 120;
				add(newGame);
				add(musicIcon);
				add(exitButton);
				add(credits);
				add(introVideo);
				theCurrentLevel = Elemental.achievements.getStoryLevelReached();
			} else {
				videoThing = new UberAwesomeVideo(1);
				toContinueOverlapped = false;
				newGameOverlapped = false;
				introVideoOverlapped = false;
				editorOverlapped = false;
				
				var level:String = localInformation.data["currentLevel"];
				if (level != null) {
					theCurrentLevel = int(level);
				} else {
					localInformation.data["currentLevel"] = "1";
					localInformation.flush();
					theCurrentLevel = int(localInformation.data["currentLevel"]);
				}
				playerPuzzles = new FlxSprite(162, 440, Res.playerPuzzlesButton);
				mapEditor = new FlxSprite(162, 500, Res.editorButton);
				
				
				FlxG.mouse.show();
				if (localInformation.data["returningUser"] != null) {
					add(toContinue);
				} 
				add(newGame);
				add(credits);
				add(introVideo);
				add(musicIcon);
				add(mapEditor);
				add(playerPuzzles);
			}
			

			hoverFilter = new GlowFilter(0xFFDF75, 0.6, 10, 10, 3);
		}
		
		public override function update():void { 
			super.update(); 
			
			if (toContinue.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!toContinueOverlapped) {
					toContinue.setFilter(hoverFilter);
					toContinueOverlapped = true;
				}
			} else {
				if (toContinueOverlapped) {
					toContinue.removeFilter();
					toContinueOverlapped = false;
				}
			}
			
			if (newGame.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!newGameOverlapped) {
					newGame.setFilter(hoverFilter);
					newGameOverlapped = true;
				}
			} else {
				if (newGameOverlapped) {
					newGame.removeFilter();
					newGameOverlapped = false;
				}
			}
			if (credits.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!creditsOverlapped) {
					credits.setFilter(hoverFilter);
					creditsOverlapped = true;
				}
			} else {
				if (creditsOverlapped) {
					credits.removeFilter();
					creditsOverlapped = false;
				}
			}
			
			
			if (introVideo.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!introVideoOverlapped) {
					introVideo.setFilter(hoverFilter);
					introVideoOverlapped = true;
				}
			} else {
				if (introVideoOverlapped) {
					introVideo.removeFilter();
					introVideoOverlapped = false;
				}
			}
			
			if (Elemental.MODE != Elemental.FACEBOOK_MODE) {
				
				if (playerPuzzles.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (!playerPuzzlesOverlapped) {
						playerPuzzles.setFilter(hoverFilter);
						playerPuzzlesOverlapped = true;
					}
				} else {
					if (playerPuzzlesOverlapped) {
						playerPuzzles.removeFilter();
						playerPuzzlesOverlapped = false;
					}
				}
				
				if (mapEditor.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (!editorOverlapped) {
						mapEditor.setFilter(hoverFilter);
						editorOverlapped = true;
					}
				} else {
					if (editorOverlapped) {
						mapEditor.removeFilter();
						editorOverlapped = false;
					}
				}
				
			} else {
				if (exitButton.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (!exitToRoomOverlapped) {
						exitButton.setFilter(hoverFilter);
						exitToRoomOverlapped = true;
					}
				} else {
					if (exitToRoomOverlapped) {
						exitButton.removeFilter();
						exitToRoomOverlapped = false;
					}
				}
			}
			
			if (FlxG.mouse.justPressed() && toContinue.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (FlxG.music != null && FlxG.music.active) {
					FlxG.music.stop();
				}
				FlxG.switchState(new LootState(theCurrentLevel)); 
			}
			
			if (FlxG.mouse.justPressed() && newGame.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (FlxG.music != null && FlxG.music.active) {
					FlxG.music.stop();
				}
				localInformation.data["currentLevel"] = "1";
				localInformation.data["returningUser"] = "true";
				localInformation.flush();
				FlxG.switchState(new GameState(1));
			}
			
			
			if (FlxG.mouse.justPressed() && introVideo.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (FlxG.music != null && FlxG.music.active) {
					FlxG.music.stop();
				}
				videoThing.play();
				FlxG.switchState(videoThing);
			}
			
			if (FlxG.mouse.justPressed() && credits.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (FlxG.music != null && FlxG.music.active) {
					FlxG.music.stop();
				}
				FlxG.switchState(new CreditState());
			}
			
			if (Elemental.MODE != Elemental.FACEBOOK_MODE) {
				if (FlxG.mouse.justPressed() && playerPuzzles.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (FlxG.music != null && FlxG.music.active) {
						FlxG.music.stop();
					}
					FlxG.switchState(new LoadPuzzlesState());
				}
				
				if (FlxG.mouse.justPressed() && mapEditor.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (FlxG.music != null && FlxG.music.active) {
						FlxG.music.stop();
					}
					FlxG.switchState(new MapEditorState(null, false));
				}	
			} else {
				if (FlxG.mouse.justPressed() && exitButton.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
					if (FlxG.music != null && FlxG.music.active) {
						FlxG.music.stop();
					}
					FlxG.switchState(new PlayerRoomState());
				}
			}
			
			musicIcon.updateMusic(Res.menuMusicS);
		}
	}
}